package com.assistant.controller;

import com.alibaba.fastjson.JSONObject;
import com.assistant.model.TextValueDto;
import com.assistant.service.es.ElasticOperatorService;
import com.assistant.util.DateUtil;
import com.assistant.util.Decrypt;
import com.assistant.util.JsonUtil;
import com.assistant.vo.req.RobotValidReq;
import com.assistant.vo.resp.AjaxResult;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/robot",produces={"application/json;charset=UTF-8"})
@Slf4j
public class RobotController {

    @PostMapping(value = "/valid")
    public JSONObject valid(@RequestBody RobotValidReq req){
        log.info(JsonUtil.toJson(req));
        String encrypt = req.getEncrypt();
        Decrypt d = new Decrypt("ZqSLRv7mKkgb8yU1ObCfkbP53wOVRhqQ");
        JSONObject jsonObject = new JSONObject();
        try {
            String decrypt = d.decrypt(encrypt);
            log.info("decrypt = {}",decrypt);
            JSONObject msg = JSONObject.parseObject(decrypt);
            jsonObject.put("challenge",msg.get("challenge"));
        } catch (Exception e) {
            log.error("报错",e);
            jsonObject.put("error",e.getMessage());
        }
        return jsonObject;
    }

    @PostMapping(value = "/cardMsg")
    public JSONObject cardMsg(@RequestBody JSONObject req){
        log.info(JsonUtil.toJson(req));
        JSONObject jsonObject = new JSONObject();
        JSONObject action = req.getJSONObject("action");
        JSONObject value = action.getJSONObject("value");
        String element = value.getString("key");
        jsonObject.put("type","template");
        JSONObject data = new JSONObject();
        data.put("template_id","ctp_AA85KMMZvsxy");
        JSONObject templateVariable = new JSONObject();
        data.put("template_variable",templateVariable);
        templateVariable.put("title","我改了标题-"+ DateUtil.getSimpleYMDHmsByDate(new Date()));
        if (element.equals("button")){
            List<TextValueDto> user = Lists.newArrayList(new TextValueDto("王云","1"),new TextValueDto("易桂林","2"),new TextValueDto("易翰林","3"));
            templateVariable.put("user",user);
            templateVariable.put("initUser","1");
            List<TextValueDto> address = Lists.newArrayList(new TextValueDto("1","1"),new TextValueDto("2","2"),new TextValueDto("3","3"));
            templateVariable.put("address",address);
            templateVariable.put("initAddress","1");
        }else if (element.equals("user")){
            List<TextValueDto> user = Lists.newArrayList(new TextValueDto("王云","1"),new TextValueDto("易桂林","2"),new TextValueDto("易翰林","3"));
            templateVariable.put("user",user);
            templateVariable.put("initUser","1");
            List<TextValueDto> address = Lists.newArrayList(new TextValueDto("a","a"),new TextValueDto("b","b"),new TextValueDto("c","c"));
            templateVariable.put("address",address);
            templateVariable.put("initAddress","b");
        }else if (element.equals("address")){
            List<TextValueDto> user = Lists.newArrayList(new TextValueDto("王云","1"),new TextValueDto("易桂林","2"),new TextValueDto("易翰林","3"));
            templateVariable.put("user",user);
            templateVariable.put("initUser","1");
            List<TextValueDto> address = Lists.newArrayList(new TextValueDto("A","A"),new TextValueDto("B","B"),new TextValueDto("C","C"));
            templateVariable.put("address",address);
            templateVariable.put("initAddress","C");
        }else {

        }
        jsonObject.put("data",data);
        return jsonObject;
    }

}
